package com.mk.service.platform;

import act.controller.Controller;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.Api;
import com.github.d925529.apidoc.annotation.ApiMethod;
import com.github.d925529.apidoc.annotation.ApiParam;
import com.github.d925529.apidoc.annotation.ApiReturn;
import com.mk.domain.CmResource;
import com.mk.domain.CmResourceFolder;
import com.mk.exception.UserException;
import com.mk.interceptor.ITranslate;
import com.mk.util.S2;
import org.osgl.mvc.annotation.*;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.List;

@Controller("cmResourceFolder")
@Api(group = "pl",title = "文件夹", path = "/cmResourceFolder")
@SuppressWarnings("unused")
public class CmResourceFolderService extends BasePlatformService {
    @Inject
    private CmResourceFolder.Mapper cmResourceFolderMapper;
    @Inject
    private CmResource.Mapper cmResourceMapper;


    @With({ILogin.class, ITranslate.class})
    @GetAction("getListByRstype.json")
    @ApiMethod(title = "查询文件夹列表", path = "/getListByRstype.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "rstype", title = "资源类型", type = String.class)
    @ApiReturn(description = "主键", type = List.class, elementType = CmResourceFolder.class)
    public List<CmResourceFolder> getListByRstype(@NotNull String rstype) {
        CmResourceFolder folder=new CmResourceFolder();
        folder.setRstype(rstype);
        return cmResourceFolderMapper.template(folder);
    }

    @With({ILogin.class, ITranslate.class})
    @PostAction("add.json")
    @ApiMethod(title = "增加保存", path = "/add.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "cmResourceFolder", title = "", type = CmResourceFolder.class)
    @ApiReturn(description = "主键", type = String.class)
    public String add(@NotNull CmResourceFolder cmResourceFolder) {
        String id = S2.UUID();
        cmResourceFolder.setCfid(id);
        cmResourceFolderMapper.insertTemplate(cmResourceFolder);
        return id;
    }

    @With({ILogin.class, ITranslate.class})
    @PutAction("update.json")
    @ApiMethod(title = "修改保存", path = "/update.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "cmResourceFolder", title = "", type = CmResourceFolder.class)
    @ApiReturn(description = "修改了的数量", type = int.class)
    public int update(@NotNull CmResourceFolder cmResourceFolder) {
        return cmResourceFolderMapper.updateTemplateById(cmResourceFolder);
    }

    @With({ILogin.class, ITranslate.class})
    @DeleteAction("deleteByid.json")
    @ApiMethod(title = "删除指定资源", path = "/deleteByid.json", method = ApiHttpMethod.DELETE)
    @ApiParam(name = "cfid", title = "删除指定资源", type = String.class)
    @ApiReturn(description = "返回行数", type = int.class)
    public int deleteByid(@NotNull String cfid){
        int cnt = cmResourceMapper.selectCntByCfid(cfid);
        if(cnt>0){
            throw new UserException("文件夹下还有资源，无法删除");
        }
        return cmResourceFolderMapper.deleteById(cfid);
    }
}
